require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIHeroHandbookReward = {};
HelperFunc.SetUIEnv(UIHeroHandbookReward)
local this = UIHeroHandbookReward;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.hasInit = false;

this.btnBgClose = nil;
this.txtTitleName = nil;
---@type GList
this.objItemGrid = nil;
this.txtTip = nil;

this.items={};

this.crystalConfig = nil;
this.itemConfig = nil;
this.inf_title = nil

function UIHeroHandbookReward.Init()--todo 应该小写 暂时先大写
    if crystalConfig==nil then
        crystalConfig = G.dataTable["t_crystal"];
    end
    if itemConfig== nil then
        itemConfig =G.dataTable["t_item"];
    end
    local name,package = getUIName(PanelResNames.UIHeroHandbookReward);
    panelMgr:ShowUI(name,package, this.onCreate, Config.useLocalUI);
end
---@param obj GComponent
function UIHeroHandbookReward.onCreate(obj)
    --gameObject = obj;
    --transform = obj.transform;
    --LuaBehaviour = transform:GetComponent('LuaBehaviour');

    if not hasInit then
        hasInit = true;
        btnBgClose = obj:GetChild("BgClose")
        txtTitleName = obj:GetChild("TitleName")
        objItemGrid = obj:GetChild("ItemSv")
        txtTip = obj:GetChild("TipSv")
        inf_title = obj:GetChild("inf_title")
        this.addEvent();
    end
    this.refresh();
end

function UIHeroHandbookReward.addEvent()
    btnBgClose.onClick:Add(this.onClickClose)
    --LuaBehaviour:AddClick(btnBgClose.gameObject, this.onClickClose);

end

function UIHeroHandbookReward.removeEvent()
    btnBgClose.onClick:Clear()
    --LuaBehaviour:RemoveClick(btnBgClose.gameObject);
end

function UIHeroHandbookReward.LocalListenEvent()

end

function UIHeroHandbookReward.LocalCloseEvent()

end

function UIHeroHandbookReward.OnDestroy()
    this.removeEvent();
end

function UIHeroHandbookReward.OnEnable()
    this.LocalListenEvent();
end

function UIHeroHandbookReward.OnDisable()
    this.LocalCloseEvent();
end


function UIHeroHandbookReward.refresh()
    --刷新函数
    local level= logicMgr.HeroManager.getCrystalInfo().level;
    local cg= crystalConfig[tostring(level)];
    txtTitleName.text = getLanguage(cg.f_Name);
    inf_title.text = string.format(getLanguage("Phases_title"),level)
    txtTip.text = string.gsub(getLanguage("PhasesExplain"),"\\n","\n");
    if cg~=nil then
        local rewards = decodeJsonStrToTabel(cg.f_Reward);
        objItemGrid.scrollPane.touchEffect = #rewards > 6
        objItemGrid:RemoveChildrenToPool()
        for k,v in pairs(rewards) do
            local ctrItem = CtrlManager.GetCtrl(PanelNames.Item);
            local itemCg = itemConfig[tostring(v[1])];
            if ctrItem~=nil and itemCg~=nil then
                local item = objItemGrid:AddItemFromPool()
                local data ={};
                data["icon"] =itemCg.f_Icon;
                data["num"] = v[2];
                data["quality"] = itemCg.f_Quality;
                data["itemModelId"] = itemCg.f_itemModelId
                ctrItem.initData(item,data);
            else
                logError("找不到 item config , id:"..v[1])
            end
        end
    end
end


function UIHeroHandbookReward.onClickClose()
    closeUI(PanelNames.UIHeroHandbookReward);
end



return UIHeroHandbookReward;
